//
//  BoutiqueTraViewController.m
//  Travel_M_Travel
//
//  Created by APPLE10 on 15-3-31.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "BoutiqueTraViewController.h"
#import "RPSlidingMenu.h"
#import "NetworkingHandler.h"
#import "BoutoqueDataSource.h"
#import "SDRefreshStart.h"
#import "JourneyWayPointsViewController.h"
@interface BoutiqueTraViewController ()<UICollectionViewDataSource,UICollectionViewDelegate, SDRefreshStartDelegate>
@property (nonatomic , retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic , retain)NSString *addCell;
@property (nonatomic , assign)NSInteger count;

@end

@implementation BoutiqueTraViewController

-(void)dealloc
{
    [_tableView release];
    [_arr release];
    [_collectionView release];
    [_addCell release];
    [_type release];
    [_idNumber release];
    [super dealloc];
    
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.page = 1;
    }
    return self;
}

- (void)netHandler
{
    NSString *str =[[NSString stringWithFormat: @"http://api.breadtrip.com/destination/place/%@/%@/trips/",self.type,self.idNumber] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        self.arr = [NSMutableArray array];
        self.addCell = [resultDic objectForKey:@"next_start"];
        for (NSMutableDictionary *dic in [resultDic objectForKey:@"items"]) {
            BoutoqueDataSource *boutoData = [[BoutoqueDataSource alloc]initWithDictionary:dic];
            [self.arr addObject:boutoData];
            [boutoData release];
        }
        [_collectionView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIView *viewS = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64)];
    viewS.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewS];
    [viewS release];
    
    [self netHandler];
    
    RPSlidingMenuLayout *layout = [[RPSlidingMenuLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, viewS.frame.size.height) collectionViewLayout:layout];
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    [self.collectionView registerClass:[RPSlidingMenuCell class] forCellWithReuseIdentifier:@"reuse"];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [viewS addSubview:self.collectionView];
    
    [SDRefreshStart SDRefeshTableView:self.collectionView delegate:self];
}

#pragma mark collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RPSlidingMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    BoutoqueDataSource *boutoData = [self.arr objectAtIndex:indexPath.item];
    cell.textLabel.text = boutoData.name;
    NSString *str = [[NSString stringWithFormat:@"%@",boutoData.recommendations] stringByAppendingString:@"喜欢"];
    cell.detailTextLabel.text = str;
    [cell.backgroundImageView setImageWithURL:[NSURL URLWithString:boutoData.cover_image_w640]];
    return cell;
}

//cell点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JourneyWayPointsViewController *jourWayVC = [[JourneyWayPointsViewController alloc]init];
    BoutoqueDataSource *boutoData = [self.arr objectAtIndex:indexPath.item];
    jourWayVC.idNumber = boutoData.idNumber;
    jourWayVC.target_id = self.idNumber;
    jourWayVC.target_type = self.type;
    
    self.navigationController.navigationBarHidden = YES;
    [self.navigationController pushViewController:jourWayVC animated:YES];
    
    
    [jourWayVC release];
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == self.arr.count - 1) {
        [self footRefesh];
    }
}

//下拉刷新
-(void)headRefesh
{
    
    self.page = 1;
    [self.arr removeAllObjects];
    
    NSString *str =[[NSString stringWithFormat: @"http://api.breadtrip.com/destination/place/%@/%@/trips/",self.type,self.idNumber] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        NSLog(@"%@", resultDic);
        //        self.arr = [NSMutableArray array];
        NSArray *list = [resultDic objectForKey:@"items"];
        for (NSMutableDictionary *dic in list) {
            BoutoqueDataSource *list = [[BoutoqueDataSource alloc]initWithDictionary:dic];
            [self.arr addObject:list];
            [list release];
        }
        [self.collectionView reloadData];
    }];
    
}

//上拉加载
- (void)footRefesh
{
    NSInteger addcell = [self.addCell integerValue];
    self.count = self.count+addcell;
    
    NSString *cellCount = [NSString stringWithFormat:@"%ld",self.count];
    NSString *str =[[NSString stringWithFormat: @"http://api.breadtrip.com/destination/place/%@/%@/trips/?start=%@",self.type,self.idNumber,cellCount] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
    [NetworkingHandler requestWithUrl:str completion:^(NSDictionary *resultDic) {
        //        NSLog(@"%@", resultDic);
        NSArray *list = [resultDic objectForKey:@"items"];
        for (NSMutableDictionary *dic in list) {
            BoutoqueDataSource *boutoData = [[BoutoqueDataSource alloc]initWithDictionary:dic];
            [self.arr addObject:boutoData];
            [boutoData release];
        }
        [self.collectionView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    [self.navigationController setNavigationBarHidden:NO];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
